home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / gprof / gpfsrc07.zoo / cplusdem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-27  |  27.1 KB  |  1,391 lines

  1. /* Demangler for GNU C++ 
  2.    Copyright (C) 1989, 1992 Free Software Foundation, Inc.
  3.    written by James Clark (jjc@jclark.uucp)
  4.    
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /* This is for g++ 1.36.1 (November 6 version). It will probably
  20.    require changes for any other version.
  21.  
  22.    Modified for g++ 1.36.2 (November 18 version).
  23.  
  24.    Modified for g++ 1.90.06 (December 31 version).
  25.  
  26.    Modified for g++ 1.95.03 (November 13 verison).  */
  27.  
  28. /* This file exports one function
  29.  
  30.    char *cplus_demangle (const char *name)
  31.  
  32.    If NAME is a mangled function name produced by GNU C++, then
  33.    a pointer to a malloced string giving a C++ representation
  34.    of the name will be returned; otherwise NULL will be returned.
  35.    It is the caller's responsibility to free the string which
  36.    is returned.
  37.  
  38.    For example,
  39.    
  40.    cplus_demangle ("_foo__1Ai")
  41.    
  42.    returns
  43.  
  44.    "A::foo(int)"
  45.  
  46.    This file imports xmalloc and xrealloc, which are like malloc and
  47.    realloc except that they generate a fatal error if there is no
  48.    available memory. */
  49.  
  50. /* #define nounderscore 1 /* define this is names don't start with _ */
  51.  
  52. #include <stdio.h>
  53. #include <ctype.h>
  54.  
  55. #ifdef USG
  56. #include <memory.h>
  57. #include <string.h>
  58. #else
  59. #include <strings.h>
  60. #define memcpy(s1, s2, n) bcopy ((s2), (s1), (n))
  61. #define memcmp(s1, s2, n) bcmp ((s2), (s1), (n))
  62. #define strchr index 
  63. #define strrchr rindex
  64. #endif
  65.  
  66. /* This is '$' on systems where the assembler can deal with that.
  67.    Where the assembler can't, it's '.' (but on many systems '.' is
  68.    used for other things).  */
  69. #if !defined (CPLUS_MARKER)
  70. #define CPLUS_MARKER '$'
  71. #endif
  72.  
  73. #ifndef __STDC__
  74. #define const
  75. #endif
  76.  
  77. #ifdef __STDC__
  78. extern char *cplus_demangle (const char *type);
  79. #else
  80. extern char *cplus_demangle ();
  81. #endif
  82.  
  83. #ifdef __STDC__
  84. extern char *xmalloc (int);
  85. extern char *xrealloc (char *, int);
  86. extern void free (char *);
  87. #else
  88. extern char *xmalloc ();
  89. extern char *xrealloc ();
  90. extern void free ();
  91. #endif
  92.  
  93. static char **typevec = 0;
  94. static int ntypes = 0;
  95. static int typevec_size = 0;
  96.  
  97. static struct {
  98.   const char *in;
  99.   const char *out;
  100. } optable[] = {
  101.   "nw", " new",            /* new (1.92, ansi) */
  102.   "dl", " delete",        /* new (1.92, ansi) */
  103.   "new", " new",        /* old (1.91) */
  104.   "delete", " delete",        /* old (1.91) */
  105.   "ne", "!=",            /* old, ansi */
  106.   "eq", "==",            /* old, ansi */
  107.   "ge", ">=",            /* old, ansi */
  108.   "gt", ">",            /* old, ansi */
  109.   "le", "<=",            /* old, ansi */
  110.   "lt", "<",            /* old, ansi */
  111.   "plus", "+",            /* old */
  112.   "pl", "+",            /* ansi */
  113.   "apl", "+=",            /* ansi */
  114.   "minus", "-",            /* old */
  115.   "mi", "-",            /* ansi */
  116.   "ami", "-=",            /* ansi */
  117.   "mult", "*",            /* old */
  118.   "ml", "*",            /* ansi */
  119.   "aml", "*=",            /* ansi */
  120.   "convert", "+",        /* old (unary +) */
  121.   "negate", "-",        /* old (unary -) */
  122.   "trunc_mod", "%",        /* old */
  123.   "md", "%",            /* ansi */
  124.   "amd", "%=",            /* ansi */
  125.   "trunc_div", "/",        /* old */
  126.   "dv", "/",            /* ansi */
  127.   "adv", "/=",            /* ansi */
  128.   "truth_andif", "&&",        /* old */
  129.   "aa", "&&",            /* ansi */
  130.   "truth_orif", "||",        /* old */
  131.   "oo", "||",            /* ansi */
  132.   "truth_not", "!",        /* old */
  133.   "nt", "!",            /* ansi */
  134.   "postincrement", "++",    /* old */
  135.   "pp", "++",            /* ansi */
  136.   "postdecrement", "--",    /* old */
  137.   "mm", "--",            /* ansi */
  138.   "bit_ior", "|",        /* old */
  139.   "or", "|",            /* ansi */
  140.   "aor", "|=",            /* ansi */
  141.   "bit_xor", "^",        /* old */
  142.   "er", "^",            /* ansi */
  143.   "aer", "^=",            /* ansi */
  144.   "bit_and", "&",        /* old */
  145.   "ad", "&",            /* ansi */
  146.   "aad", "&=",            /* ansi */
  147.   "bit_not", "~",        /* old */
  148.   "co", "~",            /* ansi */
  149.   "call", "()",            /* old */
  150.   "cl", "()",            /* ansi */
  151.   "cond", "?:",            /* old */
  152.   "alshift", "<<",        /* old */
  153.   "ls", "<<",            /* ansi */
  154.   "als", "<<=",            /* ansi */
  155.   "arshift", ">>",        /* old */
  156.   "rs", ">>",            /* ansi */
  157.   "ars", ">>=",            /* ansi */
  158.   "component", "->",        /* old */
  159.   "rf", "->",            /* ansi */
  160.   "indirect", "*",        /* old */
  161.   "method_call", "->()",    /* old */
  162.   "addr", "&",            /* old (unary &) */
  163.   "array", "[]",        /* old */
  164.   "vc", "[]",            /* ansi */
  165.   "compound", ",",        /* old */
  166.   "cm", ",",            /* ansi */
  167.   "nop", "",            /* old (for operator=) */
  168.   "as", "=",            /* ansi */
  169.   "cond", "?:",            /* old */
  170.   "cn", "?:",            /* psuedo-ansi */
  171.   "max", ">?",            /* old */
  172.   "mx", ">?",            /* psuedo-ansi */
  173.   "min", "<?",            /* old */
  174.   "mn", "<?",            /* psuedo-ansi */
  175. };
  176.  
  177. /* Beware: these aren't '\0' terminated. */
  178.  
  179. typedef struct {
  180.   char *b;            /* pointer to start of string */
  181.   char *p;            /* pointer after last character */
  182.   char *e;            /* pointer after end of allocated space */
  183. } string;
  184.  
  185. #ifdef __STDC__
  186. static void string_need (string *s, int n);
  187. static void string_delete (string *s);
  188. static void string_init (string *s);
  189. static void string_clear (string *s);
  190. static int string_empty (string *s);
  191. static void string_append (string *p, const char *s);
  192. static void string_appends (string *p, string *s);
  193. static void string_appendn (string *p, const char *s, int n);
  194. static void string_prepend (string *p, const char *s);
  195. static void string_prepends (string *p, string *s);
  196. static void string_prependn (string *p, const char *s, int n);
  197. static int get_count (const char **type, int *count);
  198. static int do_args (const char **type, string *decl);
  199. static int do_type (const char **type, string *result);
  200. static int do_arg (const char **type, string *result);
  201. static int do_args (const char **type, string *decl);
  202. static int do_cuv_prefix (const char **type, string *result, int *non_empty);
  203. static int do_builtin_type (const char **type, string *result, int *non_empty);
  204. static int do_template_args (const char **type, string *result);
  205. static void munge_function_name (string *name);
  206. static void remember_type (const char *type, int len);
  207. #else
  208. static void string_need ();
  209. static void string_delete ();
  210. static void string_init ();
  211. static void string_clear ();
  212. static int string_empty ();
  213. static void string_append ();
  214. static void string_appends ();
  215. static void string_appendn ();
  216. static void string_prepend ();
  217. static void string_prepends ();
  218. static void string_prependn ();
  219. static int get_count ();
  220. static int do_args ();
  221. static int do_type ();
  222. static int do_arg ();
  223. static int do_args ();
  224. static int do_cuv_prefix ();
  225. static int do_builtin_type ();
  226. static int do_template_args ();
  227. static void munge_function_name ();
  228. static void remember_type ();
  229. #endif
  230.  
  231. int
  232. get_simple_count (type, res)
  233.      char **type;
  234.      int *res;
  235. {
  236.   int n = 0, success = 1;;
  237.   
  238.   do
  239.     {
  240.       n *= 10;
  241.       n += **type - '0';
  242.       *type += 1;
  243.     } 
  244.   while (isdigit (**type));
  245.   if (strlen (*type) < n)
  246.     {
  247.       success = 0;
  248.     }
  249.  
  250.   *res = n;
  251.   return success;
  252. }
  253.  
  254. char *
  255. cplus_demangle (type)
  256.      const char *type;
  257. {
  258.   string decl;
  259.   int n;
  260.   int success = 0;
  261.   int constructor = 0;
  262.   int destructor = 0;
  263.   int static_type = 0;
  264.   int const_flag = 0;
  265.   int i;
  266.   const char *p;
  267. #ifndef LONGERNAMES
  268.   const char *premangle;
  269. #endif
  270.  
  271.   if (type == NULL || *type == '\0')
  272.     return NULL;
  273. #ifndef nounderscore
  274.   if (*type++ != '_')
  275.     return NULL;
  276. #endif
  277.   p = type;
  278.   while (*p != '\0' && !(*p == '_' && p[1] == '_'))
  279.     p++;
  280.   if (*p == '\0')
  281.     {
  282.       /* destructor */
  283.       if (type[0] == '_' && type[1] == CPLUS_MARKER && type[2] == '_')
  284.     {
  285.       destructor = 1;
  286.       p = type;
  287.     }
  288.       /* virtual table "_vt$"  */
  289.       else if (type[0] == '_' && type[1] == 'v' && type[2] == 't' && type[3] == CPLUS_MARKER)
  290.     {
  291.       int n = strlen (type + 4) + 14 + 1;
  292.       char *tem = (char *) xmalloc (n);
  293.       strcpy (tem, type + 4);
  294.       strcat (tem, " virtual table");
  295.       return tem;
  296.     }
  297.       /* static data member */
  298.       else if (type[0] == '_' && type[1] != '_' && (strchr (type, CPLUS_MARKER) != NULL))
  299.     {
  300.       static_type = 1;
  301.       p = type + 1;
  302.     }
  303.       else return NULL;
  304.     }
  305.  
  306.   string_init (&decl);
  307.  
  308.   if (static_type)
  309.     {
  310.       if (!isdigit (p[0]) && ('t' != p[0]))
  311.     {
  312.       string_delete (&decl);
  313.       return NULL;
  314.     }
  315.     }
  316.   else if (destructor)
  317.     {
  318.       if (!isdigit (p[3])&& ('t' != p[3]))
  319.     {
  320.       string_delete (&decl);
  321.       return NULL;
  322.     }
  323.       p += 3;
  324.     }
  325.   else if (p == type)
  326.     {
  327.       if (!isdigit (p[2]) && ('t' != p[2]))
  328.     {
  329.       p += 1;
  330.       while (*p != '\0' && !(*p == '_' && p[1] == '_'))
  331.         p++;
  332.       string_appendn (&decl, type, p - type);      
  333.       string_appendn (&decl, "", 1);
  334.       munge_function_name (&decl);
  335.       if (decl.b[0] == '_')
  336.         decl.p--;
  337.       p += 2;
  338.     }
  339.       else
  340.     {
  341.       constructor = 1;
  342.       p += 2;
  343.     }
  344.     }
  345.   else
  346.     {
  347.       string_appendn (&decl, type, p - type);
  348.       decl.p[0] = '0';
  349.       munge_function_name (&decl);
  350.       p += 2;
  351.     }
  352.  
  353. #ifndef LONGERNAMES
  354.   premangle = p;
  355. #endif
  356.   switch (*p)
  357.     {
  358.     case 'C':
  359.       /* a const member function */
  360.       p += 1;
  361.       const_flag = 1;
  362.       if (*p == 't')
  363.     goto template;
  364.       if (!isdigit (*p))
  365.     {
  366.       string_delete (&decl);
  367.       return NULL;
  368.     }
  369.       /* fall through */
  370.     case '0':
  371.     case '1':
  372.     case '2':
  373.     case '3':
  374.     case '4':
  375.     case '5':
  376.     case '6':
  377.     case '7':
  378.     case '8':
  379.     case '9':
  380.       n = 0;
  381.       do
  382.     {
  383.       n *= 10;
  384.       n += *p - '0';
  385.       p += 1;
  386.     }
  387.       while (isdigit (*p));
  388.       if (strlen (p) < n)
  389.     {
  390.       string_delete (&decl);
  391.       return NULL;
  392.     }
  393.       if (constructor || destructor)
  394.     {
  395.       string_appendn (&decl, p, n);
  396.       string_append (&decl, "::");
  397.       if (destructor)
  398.         string_append(&decl, "~");
  399.       string_appendn (&decl, p, n);
  400.     }
  401.       else
  402.     {
  403.       string_prepend (&decl, "::");
  404.       string_prependn (&decl, p, n);
  405.     }
  406.       p += n;
  407. #ifndef LONGERNAMES
  408.       remember_type (premangle, p - premangle);
  409. #endif
  410.       if (static_type)
  411.     {
  412.       string_append(&decl, p+1);
  413.       p += strlen(p);
  414.       success = 1;
  415.     }
  416.       else
  417.     success = do_args (&p, &decl);
  418.       if (const_flag)
  419.     string_append (&decl, " const");
  420.       break;
  421.     case 'F':
  422.       p += 1;
  423.       success = do_args (&p, &decl);
  424.       break;
  425.     /* template additions */
  426.     case 't':
  427.     template:
  428.       p += 1;
  429.       {
  430.     int r;
  431.     string tname;
  432.     string trawname;
  433.     
  434.     string_init(&tname);
  435.     string_init(&trawname);
  436.  
  437.     /* get template name */
  438.     if (!get_simple_count (&p, &r))
  439.       {
  440.         success = 0;
  441.         break;
  442.       }
  443.     string_appendn (&tname, p, r);
  444.     string_appendn (&trawname, p, r);
  445.     p += r;
  446.     string_append (&tname, "<");
  447.     success = do_template_args (&p, &tname);
  448.     if (!success)
  449.       {
  450.         string_delete (&tname);
  451.         string_delete (&trawname);
  452.         break;
  453.       }
  454.     string_append (&tname, ">::");
  455.     if (destructor)
  456.       string_append(&tname, "~");
  457.     if (constructor || destructor)
  458.       string_appends (&tname, &trawname);
  459.     string_delete(&trawname);
  460.  
  461.     if (!success) {
  462.       string_delete(&tname);
  463.       return 0;
  464.     }
  465.     string_prepends (&decl, &tname);
  466.     string_delete (&tname);
  467.  
  468.     if (static_type)
  469.       {
  470.         string_append (&decl, p+1);
  471.         p += strlen (p);
  472.         success = 1;
  473.       }
  474.     else
  475.       success = do_args (&p, &decl);
  476.     if (const_flag)
  477.       string_append (&decl, " const");
  478.       }
  479.       break;
  480.     }
  481.  
  482.   for (i = 0; i < ntypes; i++)
  483.     if (typevec[i] != NULL)
  484.       free (typevec[i]);
  485.   ntypes = 0;
  486.   if (typevec != NULL)
  487.     {
  488.       free ((char *)typevec);
  489.       typevec = NULL;
  490.       typevec_size = 0;
  491.     }
  492.  
  493.   if (success)
  494.     {
  495.       string_appendn (&decl, "", 1);
  496.       return decl.b;
  497.     }
  498.   else
  499.     {
  500.       string_delete (&decl);
  501.       return NULL;
  502.     }
  503. }
  504.  
  505. static int
  506. get_count (type, count)
  507.      const char **type;
  508.      int *count;
  509. {
  510.   if (!isdigit (**type))
  511.     return 0;
  512.   *count = **type - '0';
  513.   *type += 1;
  514.   /* see flush_repeats in cp-method.c */
  515.   if (isdigit (**type))
  516.     {
  517.       const char *p = *type;
  518.       int n = *count;
  519.       do 
  520.     {
  521.       n *= 10;
  522.       n += *p - '0';
  523.       p += 1;
  524.     } 
  525.       while (isdigit (*p));
  526.       if (*p == '_')
  527.     {
  528.       *type = p + 1;
  529.       *count = n;
  530.     }
  531.     }
  532.   return 1;
  533. }
  534.  
  535. /* result will be initialised here; it will be freed on failure */
  536.  
  537. static int
  538. do_type (type, result)
  539.      const char **type;
  540.      string *result;
  541. {
  542.   int n;
  543.   int done;
  544.   int non_empty = 0;
  545.   int success;
  546.   string decl;
  547.   const char *remembered_type;
  548.  
  549.   string_init (&decl);
  550.   string_init (result);
  551.  
  552.   done = 0;
  553.   success = 1;
  554.   while (success && !done)
  555.     {
  556.       int member;
  557.       switch (**type)
  558.     {
  559.     case 'Q':
  560.       n = (*type)[1] - '0';
  561.       if (n < 0 || n > 9)
  562.         {
  563.           success = 0;
  564.           break;
  565.         }
  566.       *type += 2;
  567.       while (n-- > 0)
  568.         do_type (type, result);
  569.       break;
  570.  
  571.     case 'P':
  572.       *type += 1;
  573.       string_prepend (&decl, "*");
  574.       break;
  575.  
  576.     case 'R':
  577.       *type += 1;
  578.       string_prepend (&decl, "&");
  579.       break;
  580.  
  581.     case 'T':
  582.       *type += 1;
  583.       if (!get_count (type, &n) || n >= ntypes)
  584.         success = 0;
  585.       else
  586.         {
  587.           remembered_type = typevec[n];
  588.           type = &remembered_type;
  589.         }
  590.       break;
  591.  
  592.     case 'F':
  593.       *type += 1;
  594.       if (!string_empty (&decl) && decl.b[0] == '*')
  595.         {
  596.           string_prepend (&decl, "(");
  597.           string_append (&decl, ")");
  598.         }
  599.       if (!do_args (type, &decl) || **type != '_')
  600.         success = 0;
  601.       else
  602.         *type += 1;
  603.       break;
  604.  
  605.     case 'M':
  606.     case 'O':
  607.       {
  608.         int constp = 0;
  609.         int volatilep = 0;
  610.  
  611.         member = **type == 'M';
  612.         *type += 1;
  613.         if (!isdigit (**type))
  614.           {
  615.         success = 0;
  616.         break;
  617.           }
  618.         n = 0;
  619.         do
  620.           {
  621.         n *= 10;
  622.         n += **type - '0';
  623.         *type += 1;
  624.           } 
  625.         while (isdigit (**type));
  626.         if (strlen (*type) < n)
  627.           {
  628.         success = 0;
  629.         break;
  630.           }
  631.         string_append (&decl, ")");
  632.         string_prepend (&decl, "::");
  633.         string_prependn (&decl, *type, n);
  634.         string_prepend (&decl, "(");
  635.         *type += n;
  636.         if (member)
  637.           {
  638.         if (**type == 'C')
  639.           {
  640.             *type += 1;
  641.             constp = 1;
  642.           }
  643.         if (**type == 'V')
  644.           {
  645.             *type += 1;
  646.             volatilep = 1;
  647.           }
  648.         if (*(*type)++ != 'F')
  649.           {
  650.             success = 0;
  651.             break;
  652.           }
  653.           }
  654.         if ((member && !do_args (type, &decl)) || **type != '_')
  655.           {
  656.         success = 0;
  657.         break;
  658.           }
  659.         *type += 1;
  660.         if (constp)
  661.           {
  662.         if (non_empty)
  663.           string_append (&decl, " ");
  664.         else
  665.           non_empty = 1;
  666.         string_append (&decl, "const");
  667.           }
  668.         if (volatilep)
  669.           {
  670.         if (non_empty)
  671.           string_append (&decl, " ");
  672.         else
  673.           non_empty = 1;
  674.         string_append (&decl, "volatile");
  675.           }
  676.         break;
  677.       }
  678.  
  679.     case 'C':
  680.       if ((*type)[1] == 'P')
  681.         {
  682.           *type += 1;
  683.           if (!string_empty (&decl))
  684.         string_prepend (&decl, " ");
  685.           string_prepend (&decl, "const");
  686.           break;
  687.         }
  688.  
  689.       /* fall through */
  690.     default:
  691.       done = 1;
  692.       break;
  693.     }
  694.     }
  695.  
  696.   non_empty = 0;
  697.   if (success)
  698.     success = do_cuv_prefix (type, result, &non_empty);
  699.  
  700.   if (success)
  701.     success = do_builtin_type(type, result, &non_empty);
  702.   
  703.   if (success)
  704.     {
  705.       if (!string_empty (&decl))
  706.     {
  707.       string_append (result, " ");
  708.       string_appends (result, &decl);
  709.     }
  710.       string_delete (&decl);
  711.       return 1;
  712.     }
  713.   else
  714.     {
  715.       string_delete (&decl);
  716.       string_delete (result);
  717.       return 0;
  718.     }
  719. }
  720.  
  721. static int
  722. do_cuv_prefix (type, result, non_empty)
  723.      const char **type;
  724.      string* result;
  725.      int* non_empty;
  726. {
  727.   int success = 1;
  728.   int done = 0;
  729.   
  730.   while (success && !done)
  731.     {
  732.       switch (**type)
  733.     {
  734.     case 'C':
  735.       *type += 1;
  736.       if (*non_empty)
  737.         string_append (result, " ");
  738.       else
  739.         *non_empty = 1;
  740.       string_append (result, "const");
  741.       break;
  742.     case 'U':
  743.       *type += 1;
  744.       if (*non_empty)
  745.         string_append (result, " ");
  746.       else
  747.         *non_empty = 1;
  748.       string_append (result, "unsigned");
  749.       break;
  750.     case 'V':
  751.       *type += 1;
  752.       if (*non_empty)
  753.         string_append (result, " ");
  754.       else
  755.         *non_empty = 1;
  756.       string_append (result, "volatile");
  757.       break;
  758.     default:
  759.       done = 1;
  760.       break;
  761.     }
  762.     }
  763.   return success;
  764. }
  765.  
  766. static int
  767. do_builtin_type (type, result, non_empty)
  768.      const char **type;
  769.      string* result;
  770.      int *non_empty;
  771. {
  772.   int success = 1;
  773.   int n;
  774.   
  775.   switch (**type)
  776.     {
  777.     case '\0':
  778.     case '_':
  779.       break;
  780.     case 'v':
  781.       *type += 1;
  782.       if (*non_empty)
  783.     string_append (result, " ");
  784.       string_append (result, "void");
  785.       break;
  786.     case 'x':
  787.       *type += 1;
  788.       if (*non_empty)
  789.     string_append (result, " ");
  790.       string_append (result, "long long");
  791.       break;
  792.     case 'l':
  793.       *type += 1;
  794.       if (*non_empty)
  795.     string_append (result, " ");
  796.       string_append (result, "long");
  797.       break;
  798.     case 'i':
  799.       *type += 1;
  800.       if (*non_empty)
  801.     string_append (result, " ");
  802.       string_append (result, "int");
  803.       break;
  804.     case 's':
  805.       *type += 1;
  806.       if (*non_empty)
  807.     string_append (result, " ");
  808.       string_append (result, "short");
  809.       break;
  810.     case 'c':
  811.       *type += 1;
  812.       if (*non_empty)
  813.     string_append (result, " ");
  814.       string_append (result, "char");
  815.       break;
  816.     case 'r':
  817.       *type += 1;
  818.       if (*non_empty)
  819.     string_append (result, " ");
  820.       string_append (result, "long double");
  821.       break;
  822.     case 'd':
  823.       *type += 1;
  824.       if (*non_empty)
  825.     string_append (result, " ");
  826.       string_append (result, "double");
  827.       break;
  828.     case 'f':
  829.       *type += 1;
  830.       if (*non_empty)
  831.     string_append (result, " ");
  832.       string_append (result, "float");
  833.       break;
  834.     case 'G':
  835.       *type += 1;
  836.       if (!isdigit (**type))
  837.     {
  838.       success = 0;
  839.       break;
  840.     }
  841.       /* fall through */
  842.     case '0':
  843.     case '1':
  844.     case '2':
  845.     case '3':
  846.     case '4':
  847.     case '5':
  848.     case '6':
  849.     case '7':
  850.     case '8':
  851.     case '9':
  852.       n = 0;
  853.       do
  854.     {
  855.       n *= 10;
  856.       n += **type - '0';
  857.       *type += 1;
  858.     }
  859.       while (isdigit (**type));
  860.       if (strlen (*type) < n)
  861.     {
  862.       success = 0;
  863.       break;
  864.     }
  865.       if (*non_empty)
  866.     string_append (result, " ");
  867.       string_appendn (result, *type, n);
  868.       *type += n;
  869.       break;
  870.     case 't':
  871.       *type += 1;
  872.       /* get template name */
  873.       if (!get_simple_count (type, &n))
  874.     {
  875.       success = 0;
  876.       break;
  877.     }
  878.       if (*non_empty)
  879.     string_append (result, " ");
  880.       string_appendn (result, *type, n);
  881.       *type += n;
  882.       string_append (result, "<");
  883.       if (!do_template_args (type, result))
  884.     success = 0;
  885.       else
  886.     string_append (result, ">");
  887.       break;
  888.     default:
  889.       success = 0;
  890.       break;
  891.     }
  892.   return success;
  893. }
  894.  
  895. static int
  896. do_template_args (type, result)
  897.      const char **type;
  898.      string *result;
  899. {
  900.   int r, i;
  901.  
  902.   string temp;
  903.   int need_comma = 0;
  904.   int success;
  905.  
  906.   /* get size of template parameter list */
  907.   if (!get_count (type, &r))
  908.     return 0;
  909.   for (i = 0; i < r; i++)
  910.     {
  911.       if (need_comma)
  912.     string_append (result, ", ");
  913.       /* Z for type parameters */
  914.       if (**type == 'Z')
  915.     {
  916.       *type += 1;
  917.  
  918.       success = do_type (type, &temp);
  919.       if (success)
  920.         string_appends (result, &temp);
  921.       string_delete(&temp);
  922.       if (!success)
  923.         break;
  924.     }
  925.       /* otherwise, value parameter */
  926.       else
  927.     {
  928.       const char *old_p  = *type;
  929.       int is_pointer = 0;
  930.       int is_real = 0;
  931.       int is_integral = 0;
  932.       int done = 0;
  933.  
  934.       success = do_type (type, &temp);
  935.       if (success)
  936.         string_appends (result, &temp);
  937.       string_delete(&temp);
  938.       if (!success)
  939.         break;
  940.       string_append (result, "=");
  941.       while (*old_p && !done)
  942.         {    
  943.           switch (*old_p)
  944.         {
  945.         case 'P':
  946.         case 'R':
  947.           done = is_pointer = 1;
  948.           break;
  949.         case 'C':    /* const */
  950.         case 'U':    /* unsigned */
  951.         case 'V':    /* volatile */
  952.         case 'F':    /* function */
  953.         case 'M':    /* member function */
  954.         case 'O':    /* ??? */
  955.           old_p++;
  956.           continue;
  957.         case 'Q':    /* repetition of following */
  958.         case 'T':    /* remembered type */
  959.           abort();
  960.           break;
  961.         case 'v':    /* void */
  962.           abort();
  963.           break;
  964.         case 'x':    /* long long */
  965.         case 'l':    /* long */
  966.         case 'i':    /* int */
  967.         case 's':    /* short */
  968.         case 'c':    /* char */
  969.           done = is_integral = 1;
  970.           break;
  971.         case 'r':    /* long double */
  972.         case 'd':    /* double */
  973.         case 'f':    /* float */
  974.           done = is_real = 1;
  975.           break;
  976.         default:
  977.           abort();
  978.         }
  979.         }
  980.       if (is_integral)
  981.         {
  982.           if (**type == 'm')
  983.         {
  984.           string_appendn (result, "-", 1);
  985.           *type++;
  986.         }
  987.           while (isdigit (**type))    
  988.         {
  989.           string_appendn (result, *type, 1);
  990.           *type++;
  991.         }
  992.         }
  993.       else if (is_real)
  994.         {
  995.           if (**type == 'm')
  996.         {
  997.           string_appendn (result, "-", 1);
  998.           *type++;
  999.         }
  1000.           while (isdigit (**type))    
  1001.         {
  1002.           string_appendn (result, *type, 1);
  1003.           *type++;
  1004.         }
  1005.           if (**type == '.')    /* fraction */
  1006.         {
  1007.           string_appendn (result, ".", 1);
  1008.           *type++;
  1009.           while (isdigit (**type))    
  1010.             {
  1011.               string_appendn (result, *type, 1);
  1012.               *type++;
  1013.             }
  1014.         }
  1015.           if (**type == 'e')    /* exponent */
  1016.         {
  1017.           string_appendn (result, "e", 1);
  1018.           *type++;
  1019.           while (isdigit (**type))    
  1020.             {
  1021.               string_appendn (result, *type, 1);
  1022.               *type++;
  1023.             }
  1024.         }
  1025.         }
  1026.       else if (is_pointer)
  1027.         {
  1028.           int symbol_len;
  1029.  
  1030.           if (!get_count (type, &symbol_len))
  1031.         {
  1032.           success = 0;
  1033.           break;
  1034.         }
  1035.           string_appendn (result, *type, symbol_len);
  1036.           *type += symbol_len;
  1037.         }
  1038.     }
  1039.       need_comma = 1;
  1040.     }
  1041. }
  1042.  
  1043. /* `result' will be initialised in do_type; it will be freed on failure */
  1044.  
  1045. static int
  1046. do_arg (type, result)
  1047.      const char **type;
  1048.      string *result;
  1049. {
  1050.   const char *start = *type;
  1051.  
  1052.   if (!do_type (type, result))
  1053.     return 0;
  1054.   remember_type (start, *type - start);
  1055.   return 1;
  1056. }
  1057.  
  1058. static void
  1059. remember_type (start, len)
  1060.      const char *start;
  1061.      int len;
  1062. {
  1063.   char *tem;
  1064.  
  1065.   if (ntypes >= typevec_size)
  1066.     {
  1067.       if (typevec_size == 0)
  1068.     {
  1069.       typevec_size = 3;
  1070.       typevec = (char **) xmalloc (sizeof (char*)*typevec_size);
  1071.     }
  1072.       else
  1073.     {
  1074.       typevec_size *= 2;
  1075.       typevec = (char **) xrealloc ((char *)typevec, sizeof (char*)*typevec_size);
  1076.     }
  1077.     }
  1078.   tem = (char *) xmalloc (len + 1);
  1079.   memcpy (tem, start, len);
  1080.   tem[len] = '\0';
  1081.   typevec[ntypes++] = tem;
  1082. }
  1083.  
  1084. /* `decl' must be already initialised, usually non-empty;
  1085.    it won't be freed on failure */
  1086.  
  1087. static int
  1088. do_args (type, decl)
  1089.      const char **type;
  1090.      string *decl;
  1091. {
  1092.   string arg;
  1093.   int need_comma = 0;
  1094.  
  1095.   string_append (decl, "(");
  1096.  
  1097.   while (**type != '_' && **type != '\0' && **type != 'e' && **type != 'v')
  1098.     {
  1099.       if (**type == 'N')
  1100.     {
  1101.       int r;
  1102.       int t;
  1103.       *type += 1;
  1104.       if (!get_count (type, &r) || !get_count (type, &t) || t >= ntypes)
  1105.         return 0;
  1106.       while (--r >= 0)
  1107.         {
  1108.           const char *tem = typevec[t];
  1109.           if (need_comma)
  1110.         string_append (decl, ", ");
  1111.           if (!do_arg (&tem, &arg))
  1112.         return 0;
  1113.           string_appends (decl, &arg);
  1114.           string_delete (&arg);
  1115.           need_comma = 1;
  1116.         }
  1117.     }
  1118.       else
  1119.     {
  1120.       if (need_comma)
  1121.         string_append (decl, ", ");
  1122.       if (!do_arg (type, &arg))
  1123.         return 0;
  1124.       string_appends (decl, &arg);
  1125.       string_delete (&arg);
  1126.       need_comma = 1;
  1127.     }
  1128.     }
  1129.  
  1130.   if (**type == 'v')
  1131.     *type += 1;
  1132.   else if (**type == 'e')
  1133.     {
  1134.       *type += 1;
  1135.       if (need_comma)
  1136.     string_append (decl, ",");
  1137.       string_append (decl, "...");
  1138.     }
  1139.  
  1140.   string_append (decl, ")");
  1141.   return 1;
  1142. }
  1143.  
  1144. static void
  1145. munge_function_name (name)
  1146.      string *name;
  1147. {
  1148.   if (string_empty (name))
  1149.     return;
  1150.  
  1151.   if (name->p - name->b >= 3 
  1152.       && name->b[0] == 'o' && name->b[1] == 'p' && name->b[2] == CPLUS_MARKER)
  1153.     {
  1154.       int i;
  1155.       /* see if it's an assignment expression */
  1156.       if (name->p - name->b >= 10 /* op$assign_ */
  1157.       && memcmp (name->b + 3, "assign_", 7) == 0)
  1158.     {
  1159.       for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
  1160.         {
  1161.           int len = name->p - name->b - 10;
  1162.           if (strlen (optable[i].in) == len
  1163.           && memcmp (optable[i].in, name->b + 10, len) == 0)
  1164.         {
  1165.           string_clear (name);
  1166.           string_append (name, "operator");
  1167.           string_append (name, optable[i].out);
  1168.           string_append (name, "=");
  1169.           return;
  1170.         }
  1171.         }
  1172.     }
  1173.       else
  1174.     {
  1175.       for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
  1176.         {
  1177.           int len = name->p - name->b - 3;
  1178.           if (strlen (optable[i].in) == len 
  1179.           && memcmp (optable[i].in, name->b + 3, len) == 0)
  1180.         {
  1181.           string_clear (name);
  1182.           string_append (name, "operator");
  1183.           string_append (name, optable[i].out);
  1184.           return;
  1185.         }
  1186.         }
  1187.     }
  1188.       return;
  1189.     }
  1190.   else if (name->p - name->b >= 5 && memcmp (name->b, "type$", 5) == 0)
  1191.     {
  1192.       /* type conversion operator */
  1193.       string type;
  1194.       const char *tem = name->b + 5;
  1195.       if (do_type (&tem, &type))
  1196.     {
  1197.       string_clear (name);
  1198.       string_append (name, "operator ");
  1199.       string_appends (name, &type);
  1200.       string_delete (&type);
  1201.       return;
  1202.     }
  1203.     }
  1204.   /* ANSI.  */
  1205.   else if (name->b[2] == 'o' && name->b[3] == 'p')
  1206.     {
  1207.       /* type conversion operator.  */
  1208.       string type;
  1209.       const char *tem = name->b + 4;
  1210.       if (do_type (&tem, &type))
  1211.     {
  1212.       string_clear (name);
  1213.       string_append (name, "operator ");
  1214.       string_appends (name, &type);
  1215.       string_delete (&type);
  1216.       return;
  1217.     }
  1218.     }
  1219.   else if (name->b[2] >= 'a' && name->b[2] <= 'z'
  1220.        && name->b[3] >= 'a' && name->b[3] <= 'z')
  1221.     {
  1222.       int i;
  1223.  
  1224.       if (name->b[4] == '\0')
  1225.     {
  1226.       /* Operator.  */
  1227.       for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
  1228.         {
  1229.           if (strlen (optable[i].in) == 2
  1230.           && memcmp (optable[i].in, name->b + 2, 2) == 0)
  1231.         {
  1232.           string_clear (name);
  1233.           string_append (name, "operator");
  1234.           string_append (name, optable[i].out);
  1235.           return;
  1236.         }
  1237.         }
  1238.     }
  1239.       else
  1240.     {
  1241.       if (name->b[2] != 'a' || name->b[5] != '\0')
  1242.         return;
  1243.       /* Assignment.  */
  1244.       for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
  1245.         {
  1246.           if (strlen (optable[i].in) == 3
  1247.           && memcmp (optable[i].in, name->b + 2, 3) == 0)
  1248.         {
  1249.           string_clear (name);
  1250.           string_append (name, "operator");
  1251.           string_append (name, optable[i].out);
  1252.           return;
  1253.         }
  1254.         }
  1255.     }
  1256.     }
  1257. }
  1258.  
  1259. /* a mini string-handling package */
  1260.  
  1261. static void
  1262. string_need (s, n)
  1263.      string *s;
  1264.      int n;
  1265. {
  1266.   if (s->b == NULL)
  1267.     {
  1268.       if (n < 32)
  1269.     n = 32;
  1270.       s->p = s->b = (char *) xmalloc (n);
  1271.       s->e = s->b + n;
  1272.     }
  1273.   else if (s->e - s->p < n)
  1274.     {
  1275.       int tem = s->p - s->b;
  1276.       n += tem;
  1277.       n *= 2;
  1278.       s->b = (char *) xrealloc (s->b, n);
  1279.       s->p = s->b + tem;
  1280.       s->e = s->b + n;
  1281.     }
  1282. }
  1283.  
  1284. static void
  1285. string_delete (s)
  1286.      string *s;
  1287. {
  1288.   if (s->b != NULL)
  1289.     {
  1290.       free (s->b);
  1291.       s->b = s->e = s->p = NULL;
  1292.     }
  1293. }
  1294.  
  1295. static void
  1296. string_init (s)
  1297.      string *s;
  1298. {
  1299.   s->b = s->p = s->e = NULL;
  1300. }
  1301.  
  1302. static void 
  1303. string_clear (s)
  1304.      string *s;
  1305. {
  1306.   s->p = s->b;
  1307. }
  1308.  
  1309. static int
  1310. string_empty (s)
  1311.      string *s;
  1312. {
  1313.   return s->b == s->p;
  1314. }
  1315.  
  1316. static void
  1317. string_append (p, s)
  1318.      string *p;
  1319.      const char *s;
  1320. {
  1321.   int n;
  1322.   if (s == NULL || *s == '\0')
  1323.     return;
  1324.   n = strlen (s);
  1325.   string_need (p, n);
  1326.   memcpy (p->p, s, n);
  1327.   p->p += n;
  1328. }
  1329.  
  1330. static void
  1331. string_appends (p, s)
  1332.      string *p, *s;
  1333. {
  1334.   int n;
  1335.   if (s->b == s->p)
  1336.     return;
  1337.   n = s->p - s->b;
  1338.   string_need (p, n);
  1339.   memcpy (p->p, s->b, n);
  1340.   p->p += n;
  1341. }
  1342.  
  1343. static void
  1344. string_appendn (p, s, n)
  1345.      string *p;
  1346.      const char *s;
  1347.      int n;
  1348. {
  1349.   if (n == 0)
  1350.     return;
  1351.   string_need (p, n);
  1352.   memcpy (p->p, s, n);
  1353.   p->p += n;
  1354. }
  1355.  
  1356. static void
  1357. string_prepend (p, s)
  1358.      string *p;
  1359.      const char *s;
  1360. {
  1361.   if (s == NULL || *s == '\0')
  1362.     return;
  1363.   string_prependn (p, s, strlen (s));
  1364. }
  1365.  
  1366. static void
  1367. string_prepends (p, s)
  1368.      string *p, *s;
  1369. {
  1370.   if (s->b == s->p)
  1371.     return;
  1372.   string_prependn (p, s->b, s->p - s->b);
  1373. }
  1374.  
  1375. static void
  1376. string_prependn (p, s, n)
  1377.      string *p;
  1378.      const char *s;
  1379.      int n;
  1380. {
  1381.   char *q;
  1382.  
  1383.   if (n == 0)
  1384.     return;
  1385.   string_need (p, n);
  1386.   for (q = p->p - 1; q >= p->b; q--)
  1387.     q[n] = q[0];
  1388.   memcpy (p->b, s, n);
  1389.   p->p += n;
  1390. }
  1391.